﻿using EntityFrameworkLayer.Context;
using EntityFrameworkLayer.Entities;
using EntityFrameworkLayer.Extension;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;

namespace EntityFrameworkLayer.Service
{
    public class MaterialService
    {
        /// <summary>
        /// Service method to get material by Id
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>Data row</returns>
        public DataRow GetById(int id)
        {
            using (var context = new MaterialContext())
            {
                Material membership = context.Materials
                    .Where(i => i.Id == id)
                    .SingleOrDefault();
                return membership.ToDataRow<Material>();
                //return ListExtension.ToDataRow<Material>(membership);
            }
        }

        /// <summary>
        /// Service method to get all materials
        /// </summary>
        /// <returns>Data table</returns>
        public DataTable GetAll()
        {
            using (var context = new MaterialContext())
            {
                IList<Material> members = context.Materials.ToList();

                return members.ToDataTable<Material>();
            }
        }

        /// <summary>
        /// Service method to search material by multiple parameters
        /// </summary>
        /// <param name="seriousNumber">seriousNumber</param>
        /// <param name="name">name</param>
        /// <param name="operand">AND OR operand</param>
        /// <returns>Data table</returns>
        public DataTable Search(string seriousNumber, string name, string operand)
        {
            using (var context = new MaterialContext())
            {
                List<Material> members = context.Materials.Where(i => operand == "AND" ?
                    (seriousNumber == i.SeriousNumber) &&
                    (name == i.Name) :
                    (seriousNumber == i.SeriousNumber) ||
                    (name == i.Name))
                    .ToList();

                return members.ToDataTable<Material>();
            }
        }

        /// <summary>
        /// Service method to create new material
        /// </summary>
        /// <param name="material">material model</param>
        /// <returns>true or false</returns>
        public bool Create(Material material)
        {
            using (var context = new MaterialContext())
            {
                context.Materials.Add(material);
                return context.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// Service method to update material
        /// </summary>
        /// <param name="material">material</param>
        /// <returns>true / false</returns>
        public bool Update(Material material)
        {
            using (var context = new MaterialContext())
            {
                context.Materials.Attach(material);
                context.Entry(material).State = EntityState.Modified;
                return context.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// Method to delete a material
        /// </summary>
        /// <param name="id">material id</param>
        /// <returns>true / false</returns>
        public bool Delete(int id)
        {
            using (var context = new MaterialContext())
            {
                var material = context.Materials.Find(id);
                context.Materials.Remove(material);
                return context.SaveChanges() > 0;
            }
        }
    }
}
